﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DB
{
    public class Product
    {
        public decimal ProductID;
        public string SKU;
        public string UPC;
        public string Type;
        public decimal Price;
        public decimal PriceSale;
        public decimal PricePurchase;
        public long Inventory;
        public long EbaoInventory;
        public string Size;
        public string Weight;
        public string Color;
        public string Stone;
        public string Material;
        public string Theme;
        public string Feature;
        public string Brand;
        public string Metals;
        public byte[] Status;
        public string EbaoURL;
        public string MaibeyURL;
    }
    public class ProductsDb : DbBase
    {
        private ProductsDb(DbEngineBase dbEngineBase) : base(dbEngineBase)
        {
        }

        private static ProductsDb _dbMysql;
        public static ProductsDb DbMysql
        {
            get 
            {
                return _dbMysql ??(_dbMysql = new ProductsDb(new DbEngineMysql("Server=mail.maibey.com;userid=love;password=1234567;Database=Barn;CharSet=utf8;")));
            }
        }

        public List<Product> GetAllProducts()
        {
            var strErr = "";
            var dt = this.QueryData("Select * from Products",ref strErr);
            return Get(dt);
        }
        public bool UpdateProduts(string strSql)
        {
            var strErr = "";
            return this.Exec(strSql, ref strErr);
        }
        public bool AddProdut(Product product)
        {
            var strErr = "";
            var strSql = @"Insert INTO Products 
	(`ProductId`, 
	`SKU`, 
	`UPC`, 
	`Type`, 
	`Price`, 
	`PriceSale`, 
	`PricePurchase`, 
	`Inventory`, 
	`Size`, 
	`Weight`, 
	`Color`, 
	`Stone`, 
	`Material`, 
	`Theme`, 
	`Feature`, 
	`Brand`, 
	`Metals`, 
	`Status`, 
	`EbaoURL`, 
	`MaibeyURL`, 
	`EbaoInventory`
	)
	VALUES
	('" + product.ProductID + @"', 
	'" + product.SKU + @"', 
	'" + product.UPC + @"', 
	'" + product.Type + @"', 
	'" + product.Price + @"', 
	'" + product.PriceSale + @"', 
	'" + product.PricePurchase + @"', 
	'" + product.Inventory + @"', 
	'" + product.Size + @"', 
	'" + product.Weight + @"', 
	'" + product.Color + @"', 
	'" + product.Stone + @"', 
	'" + product.Material + @"', 
	'" + product.Theme + @"', 
	'" + product.Feature + @"', 
	'" + product.Brand + @"', 
	'" + product.Metals + @"', 
	'" + product.Status + @"', 
	'" + product.EbaoURL + @"', 
	'" + product.MaibeyURL + @"', 
	'" + product.EbaoInventory + @"' )";
            return this.Exec(strSql, ref strErr);
        }
        public bool UpdateProduts(Product product)
        {
            var strErr = "";
            var strSql = "Update Products set "
                         + "Brand = '" + product.Brand + "',"
                         + "Color = '" + product.Color + "',"
                         + "Feature = '" + product.Feature + "',"
                         + "Inventory = '" + product.Inventory + "',"
                         + "Material = '" + product.Material + "',"
                         + "Metals = '" + product.Metals + "',"
                         + "Price = '" + product.Price + "',"
                         + "PricePurchase = '" + product.PricePurchase + "',"
                         + "PriceSale = '" + product.PriceSale + "',"
                         + "Size = '" + product.Size + "',"
                         + "Stone = '" + product.Stone + "',"
                         + "Theme = '" + product.Theme + "',"
                         + "Type = '" + product.Type + "',"
                         + "UPC = '" + product.UPC + "',"
                         + "Weight = '" + product.Weight + "', "
                         + "MaibeyURL = '" + product.MaibeyURL + "', "
                         + "EbaoInventory = '" + product.EbaoInventory + "' "

                         + " WHERE SKU = '" + product.SKU + "'";
            return this.Exec(strSql, ref strErr);
        }
        public bool SetProdutSku(Product product)
        {
            var strErr = "";
            var strSql = "Update Products set "
                         + "Brand = '" + product.Brand + "',"
                         + "Color = '" + product.Color + "',"
                         + "Feature = '" + product.Feature + "',"
                         + "Inventory = '" + product.Inventory + "',"
                         + "Material = '" + product.Material + "',"
                         + "Metals = '" + product.Metals + "',"
                         + "Price = '" + product.Price + "',"
                         + "PricePurchase = '" + product.PricePurchase + "',"
                         + "PriceSale = '" + product.PriceSale + "',"
                         + "Size = '" + product.Size + "',"
                         + "Stone = '" + product.Stone + "',"
                         + "Theme = '" + product.Theme + "',"
                         + "Type = '" + product.Type + "',"
                         + "UPC = '" + product.UPC + "',"
                         + "Weight = '" + product.Weight + "', "
                         + "SKU = '" + product.SKU + "', "
                         + "EbaoInventory = '" + product.EbaoInventory + "' "

                         + " WHERE EbaoURL = '" + product.EbaoURL + "'";
            return this.Exec(strSql, ref strErr);
        }

        public bool SetProdutMaibeyUrl(Product product)
        {
            var strErr = "";
            var strSql = "Update Products set "
                         + "MaibeyURL = '" + product.MaibeyURL + "' "

                         + " WHERE SKU = '" + product.SKU + "'";
            return this.Exec(strSql, ref strErr);
        }
        private List<Product> Get(DataTable dt)
        {
            var listRtn = new List<Product>();
            listRtn = ListCreater<Product>.CreateFromDataTable(dt);
            return listRtn;
        }
    }
}
